/**
 * 成品明细库存表
 */
var parameter = getRequest();
var username = parameter['username'];//参数名
var tabNumber;

//库存表
$(function () {

    //设置起始日期，
    // getreportStartDate();

    initKendoUiGrid();
    initKendoUiGrid_m();
    initKendoUiGrid_n();
    initKendoUiGrid_b();
    btn_refresh();
});

/**
 * 将数组去重
 * @param arr
 * @param attribute
 * @returns {[]}
 */
function unique(arr, attribute) {
    let new_arr = [];
    let json_arr = [];
    for (let i = 0; i < arr.length; i++) {
        if (new_arr.indexOf(arr[i][attribute]) == -1) {
            new_arr.push(arr[i][attribute]);
            json_arr.push(arr[i]);
        }
    }
    return json_arr;
}

// 构建表结构
//全部计划
function initKendoUiGrid(data) {

    var iid = new kendo.data.DataSource({data: []});
    var bill_date = new kendo.data.DataSource({data: []});
    var client_sid = new kendo.data.DataSource({data: []});
    var client_name = new kendo.data.DataSource({data: []});
    var order_code = new kendo.data.DataSource({data: []});
    var product_sid = new kendo.data.DataSource({data: []});
    var color = new kendo.data.DataSource({data: []});
    var price = new kendo.data.DataSource({data: []});
    var amount = new kendo.data.DataSource({data: []});
    var kilo = new kendo.data.DataSource({data: []});
    var remark = new kendo.data.DataSource({data: []});
    var delivery_date = new kendo.data.DataSource({data: []});
    var color_code = new kendo.data.DataSource({data: []});
    var bill_code = new kendo.data.DataSource({data: []});
    var B_ProcessName = new kendo.data.DataSource({data: []});
    var B_ProcessDate = new kendo.data.DataSource({data: []});
    var taketime = new kendo.data.DataSource({data: []});

    var dataSource = new kendo.data.DataSource({
        data: data,
        schema: {
            model: {
                fields: {
                    amount: {type: "number"},
                    kilo: {type: "number"},
                }
            }
        },
        aggregate: [
            {field: "amount", aggregate: "sum"},
            {field: "kilo", aggregate: "sum"},
        ],

        change: function (e) {
            //获取过滤条件
            let filtrer = this.filter();
            //获取所有数据
            let allData = this.data();
            //进行过滤条件的筛选
            let query = new kendo.data.Query(allData);
            //获取过过滤后的数据
            let data = query.filter(filtrer).data;
            iid.data(unique(data, 'iid'));
            client_sid.data(unique(data, 'client_sid'));
            client_name.data(unique(data, 'client_name'));
            order_code.data(unique(data, 'order_code'));
            product_sid.data(unique(data, 'product_sid'));
            color.data(unique(data, 'color'));
            remark.data(unique(data, 'remark'));
            bill_code.data(unique(data, 'bill_code'));
            B_ProcessName.data(unique(data, 'B_ProcessName'));
            B_ProcessDate.data(unique(data, 'B_ProcessDate'));
            taketime.data(unique(data, 'taketime'));

        },
        pageSize: 200,
        requestStart: function (e) {
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text == '查询' || (btn.linkbutton('options').text == '过滤')) {
                    btn.linkbutton('disable')
                }
            })
        },
        requestEnd: function (e) {
            let response = e.response;
            if (response && response['message']) {
                $.messager.show({title: '提示', msg: response['message']})
            }
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text == '查询' || (btn.linkbutton('options').text == '过滤')) {
                    btn.linkbutton('enable')
                }
            })
        }
    });
    var grid = $("#bill").kendoGrid({
        sortable: true,
        resizable: true,
        selectable: "multiple row",
        editable: false,
        filterable: true,
        title: "报表",
        columnMenu: {
            sortable: true
        },
        excel: {
            fileName: "计划单序时表.xlsx",
            proxyURL: "",
            allPages: true,
            filterable: true
        },
        pageable: {
            pageSize: 200
        },
        dataSource: dataSource,
        columns: [
            {
                field: "", title: "序号", width: 30,
                template: "<span class='row_number'></span>", width: 40
            },
            {
                field: "parent_id",
                hidden: true,
                title: '订单明细主键',
                width: 30,
                attributes: {style: 'white-space:nowrap;'},
                headerAttributes: {style: 'text-align:left;margin-right: 2px;'},
                footerAttributes: {style: 'color: red;text-align:center;'}
            },
            {
                field: 'iid',
                title: '计划单主键',
                footerTemplate: '总计:',
                width: 80,
                hidden: true,
                filterable: {multi: true, search: true, dataSource: iid},
                footerAttributes: {style: "text-align:right;"},
                attributes: {style: "text-align:center;white-space:nowrap;text-overflow:ellipsis;"},
                headerAttributes: {style: 'text-align:center;'}
            },
            {
                field: 'bill_code',
                title: '计划单号',
                footerTemplate: '总计:',
                width: 110,
                filterable: {multi: true, search: true, dataSource: bill_code},
                footerAttributes: {style: "text-align:right;"},
                attributes: {style: "text-align:center;white-space:nowrap;text-overflow:ellipsis;"},
                headerAttributes: {style: 'text-align:center;'}
            },
            {
                field: "bill_date",
                title: '开单日期',
                width: 110,
                filterable: {multi: true, search: true, dataSource: bill_date},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            // {
            //     field: "delivery_date",
            //     title: '交期',
            //     width: 75  ,
            //     filterable: {multi: true, search: true, dataSource: delivery_date},
            //     attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
            //     headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            // },
            {
                field: "client_sid",
                title: '客户编号',
                hidden: true,
                width: 100,
                filterable: {multi: true, search: true, dataSource: client_sid},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: 'client_name',
                title: '客户名称',
                width: 100,
                filterable: {multi: true, search: true, dataSource: client_name},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: 'order_code',
                title: '订单号',
                width: 120,
                filterable: {multi: true, search: true, dataSource: order_code},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: 'product_sid',
                title: '品名',
                width: 200,
                filterable: {multi: true, search: true, dataSource: product_sid},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },

            {
                field: 'color',
                title: '颜色',
                width: 100,
                filterable: {multi: true, search: true, dataSource: color},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            // {
            //     field: 'color_code',
            //     title: '色号',
            //     width: 100,
            //     filterable: {multi: true, search: true, dataSource: color},
            //     attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
            //     headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            // },
            // {
            //     // patternId
            //     field: "color_code",
            //     title: "色块",
            //     width: 50,
            //     attributes: {style: "white-space:nowrap;text-overflow:ellipsis;background-color:#= color_code?color_code:'white'# ;color:#= color_code?color_code:'white'# ;"}
            // },
            // {
            //     field: 'amount',
            //     title: '件数',
            //     width: 80,
            //     template: item => item['amount'] ? parseFloat(item['amount']).toFixed(0) : '',
            //     format: "{0:n1}",
            //     footerTemplate: '#=kendo.format("{0:n1}",sum)#',
            //     footerAttributes: {style: "text-align:right;"},
            //     attributes: {style: "text-align:right;",'class': 'kendo-custum-number'},
            //     headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            // },
            {
                field: 'price',
                title: '单价',
                width: 100,
                filterable: {multi: true, search: true, dataSource: price},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: 'kilo',
                title: '计划重量',
                width: 100,
                template: item => item['kilo'] ? parseFloat(item['kilo']).toFixed(1) : '',
                format: "{0:n1}",
                footerTemplate: '#=kendo.format("{0:n1}",sum)#',
                footerAttributes: {style: "text-align:right;"},
                attributes: {style: "text-align:right;",'class': 'kendo-custum-number'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "B_ProcessName",
                title: '当前工序',
                width: 100,
                filterable: {multi: true, search: true, dataSource: B_ProcessName},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "B_ProcessDate",
                title: '刷卡时间',
                width: 120,
                filterable: {multi: true, search: true, dataSource: B_ProcessDate},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "taketime",
                title: '耗时',
                width: 80,
                filterable: {multi: true, search: true, dataSource: taketime},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },

            {
                field: "remark",
                title: '开单备注',
                width: 200,
                filterable: {multi: true, search: true, dataSource: remark},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "finished",
                title: '完工',
                width: 50,
                hidden: true,
                filterable: {multi: true, search: true, dataSource: 'finished'},
                attributes: {style: 'white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "",
                title: '.',
                width: 10,
                filterable: {multi: true, search: true, dataSource: ''},
                attributes: {style: 'white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
        ],
        reorderable: true,
        height: '99%',
        dataBound: function () {
            var rows = this.items();
            $(rows).each(function () {
                var index = $(this).index() + 1;
                var rowLabel = $(this).find(".row_number");
                $(rowLabel).html(index);
            });
            // 行样式
            let kgrid = $("#bill").data("kendoGrid");
            let trs = kgrid.tbody.find("tr");
            trs.each(function (i, row) {
                let data = kgrid.dataItem(row);
                let finished = data.finished;
                if (finished === 1) $(row).css("color", "#0f0cd0");
            });
        },
    });
    var grid01 = $("#bill").data("kendoGrid");
    //双击事件
    grid.on('dblclick', 'tr', function () {
        //  代码
        var kGrid = $("#bill").data("kendoGrid");
        var row = kGrid.select();
        var dataItem = kGrid.dataItem(row);
        var id = dataItem['iid'];
        var parent_id = dataItem['parent_id'];
        $('#B').window('open');  // open a window
        openPlanNow(parent_id,id)
    });
    //右击事件
    grid.on('contextmenu', 'tr', function (e) {
        e.preventDefault();
        //  代码

    });
    //  单击事件
    grid.on('click', 'tr', function () {
        //  代码

    });
}

//未排计划
function initKendoUiGrid_m(data) {

    var iid = new kendo.data.DataSource({data: []});
    var bill_date = new kendo.data.DataSource({data: []});
    var client_sid = new kendo.data.DataSource({data: []});
    var client_name = new kendo.data.DataSource({data: []});
    var order_code = new kendo.data.DataSource({data: []});
    var product_sid = new kendo.data.DataSource({data: []});
    var color = new kendo.data.DataSource({data: []});
    var price = new kendo.data.DataSource({data: []});
    var amount = new kendo.data.DataSource({data: []});
    var kilo = new kendo.data.DataSource({data: []});
    var remark = new kendo.data.DataSource({data: []});
    var delivery_date = new kendo.data.DataSource({data: []});
    var color_code = new kendo.data.DataSource({data: []});
    var bill_code = new kendo.data.DataSource({data: []});


    var dataSource = new kendo.data.DataSource({
        data: data,
        schema: {
            model: {
                fields: {
                    amount: {type: "number"},
                    kilo: {type: "number"},
                }
            }
        },
        aggregate: [
            {field: "amount", aggregate: "sum"},
            {field: "kilo", aggregate: "sum"},
        ],

        change: function (e) {
            //获取过滤条件
            let filtrer = this.filter();
            //获取所有数据
            let allData = this.data();
            //进行过滤条件的筛选
            let query = new kendo.data.Query(allData);
            //获取过过滤后的数据
            let data = query.filter(filtrer).data;
            iid.data(unique(data, 'iid'));
            client_sid.data(unique(data, 'client_sid'));
            client_name.data(unique(data, 'client_name'));
            order_code.data(unique(data, 'order_code'));
            product_sid.data(unique(data, 'product_sid'));
            color.data(unique(data, 'color'));
            remark.data(unique(data, 'remark'));
            bill_code.data(unique(data, 'bill_code'));


        },
        pageSize: 200,
        requestStart: function (e) {
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text == '查询' || (btn.linkbutton('options').text == '过滤')) {
                    btn.linkbutton('disable')
                }
            })
        },
        requestEnd: function (e) {
            let response = e.response;
            if (response && response['message']) {
                $.messager.show({title: '提示', msg: response['message']})
            }
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text == '查询' || (btn.linkbutton('options').text == '过滤')) {
                    btn.linkbutton('enable')
                }
            })
        }
    });
    var grid = $("#bill_m").kendoGrid({
        sortable: true,
        resizable: true,
        selectable: "multiple row",
        editable: false,
        filterable: true,
        title: "报表",
        columnMenu: {
            sortable: true
        },
        excel: {
            fileName: "未排计划序时表.xlsx",
            proxyURL: "",
            allPages: true,
            filterable: true
        },
        pageable: {
            pageSize: 200
        },
        dataSource: dataSource,
        columns: [
            {
                field: "", title: "序号", width: 30,
                template: "<span class='row_number'></span>", width: 40
            },
            {
                field: "parent_id",
                hidden: true,
                title: '订单明细主键',
                width: 30,
                attributes: {style: 'white-space:nowrap;'},
                headerAttributes: {style: 'text-align:left;margin-right: 2px;'},
                footerAttributes: {style: 'color: red;text-align:center;'}
            },
            {
                field: 'iid',
                title: '计划单主键',
                footerTemplate: '总计:',
                width: 80,
                hidden: true,
                filterable: {multi: true, search: true, dataSource: iid},
                footerAttributes: {style: "text-align:right;"},
                attributes: {style: "text-align:center;white-space:nowrap;text-overflow:ellipsis;"},
                headerAttributes: {style: 'text-align:center;'}
            },
            {
                field: 'bill_code',
                title: '计划单号',
                footerTemplate: '总计:',
                width: 110,
                filterable: {multi: true, search: true, dataSource: bill_code},
                footerAttributes: {style: "text-align:right;"},
                attributes: {style: "text-align:center;white-space:nowrap;text-overflow:ellipsis;"},
                headerAttributes: {style: 'text-align:center;'}
            },
            {
                field: "bill_date",
                title: '开单日期',
                width: 110,
                filterable: {multi: true, search: true, dataSource: bill_date},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            // {
            //     field: "delivery_date",
            //     title: '交期',
            //     width: 75  ,
            //     filterable: {multi: true, search: true, dataSource: delivery_date},
            //     attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
            //     headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            // },
            {
                field: "client_sid",
                title: '客户编号',
                hidden: true,
                width: 100,
                filterable: {multi: true, search: true, dataSource: client_sid},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: 'client_name',
                title: '客户名称',
                width: 100,
                filterable: {multi: true, search: true, dataSource: client_name},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: 'order_code',
                title: '订单号',
                width: 120,
                filterable: {multi: true, search: true, dataSource: order_code},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: 'product_sid',
                title: '品名',
                width: 200,
                filterable: {multi: true, search: true, dataSource: product_sid},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },

            {
                field: 'color',
                title: '颜色',
                width: 100,
                filterable: {multi: true, search: true, dataSource: color},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            // {
            //     field: 'color_code',
            //     title: '色号',
            //     width: 100,
            //     filterable: {multi: true, search: true, dataSource: color},
            //     attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
            //     headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            // },
            // {
            //     // patternId
            //     field: "color_code",
            //     title: "色块",
            //     width: 50,
            //     attributes: {style: "white-space:nowrap;text-overflow:ellipsis;background-color:#= color_code?color_code:'white'# ;color:#= color_code?color_code:'white'# ;"}
            // },
            // {
            //     field: 'amount',
            //     title: '件数',
            //     width: 80,
            //     template: item => item['amount'] ? parseFloat(item['amount']).toFixed(0) : '',
            //     format: "{0:n1}",
            //     footerTemplate: '#=kendo.format("{0:n1}",sum)#',
            //     footerAttributes: {style: "text-align:right;"},
            //     attributes: {style: "text-align:right;",'class': 'kendo-custum-number'},
            //     headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            // },
            {
                field: 'price',
                title: '单价',
                width: 100,
                filterable: {multi: true, search: true, dataSource: price},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: 'kilo',
                title: '计划重量',
                width: 100,
                template: item => item['kilo'] ? parseFloat(item['kilo']).toFixed(1) : '',
                format: "{0:n1}",
                footerTemplate: '#=kendo.format("{0:n1}",sum)#',
                footerAttributes: {style: "text-align:right;"},
                attributes: {style: "text-align:right;",'class': 'kendo-custum-number'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },

            {
                field: "remark",
                title: '开单备注',
                width: 200,
                filterable: {multi: true, search: true, dataSource: remark},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "",
                title: '.',
                width: 10,
                filterable: {multi: true, search: true, dataSource: ''},
                attributes: {style: 'white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
        ],
        reorderable: true,
        height: '99%',
        dataBound: function () {
            var rows = this.items();
            $(rows).each(function () {
                var index = $(this).index() + 1;
                var rowLabel = $(this).find(".row_number");
                $(rowLabel).html(index);
            });
            // 行样式
            // let kgrid = $("#bill_m").data("kendoGrid");
            // let trs = kgrid.tbody.find("tr");
            // trs.each(function (i, row) {
            //     let data = kgrid.dataItem(row);
            //     let finished = data.finished;
            //     if (finished === 1) $(row).css("color", "#0f0cd0");
            // });
        },
    });
    var grid01 = $("#bill_m").data("kendoGrid");
    //双击事件
    grid.on('dblclick', 'tr', function () {
        //  代码
        var kGrid = $("#bill_m").data("kendoGrid");
        var row = kGrid.select();
        var dataItem = kGrid.dataItem(row);
        var id = dataItem['iid'];
        var parent_id = dataItem['parent_id'];
        $('#B').window('open');  // open a window
        openPlanNow(parent_id,id)
    });
    //右击事件
    grid.on('contextmenu', 'tr', function (e) {
        e.preventDefault();
        //  代码

    });
    //  单击事件
    grid.on('click', 'tr', function () {
        //  代码

    });
}

//已生成缸单
function initKendoUiGrid_n(data) {

    var iid = new kendo.data.DataSource({data: []});
    var bill_date = new kendo.data.DataSource({data: []});
    var client_sid = new kendo.data.DataSource({data: []});
    var client_name = new kendo.data.DataSource({data: []});
    var order_code = new kendo.data.DataSource({data: []});
    var product_sid = new kendo.data.DataSource({data: []});
    var color = new kendo.data.DataSource({data: []});
    var amount = new kendo.data.DataSource({data: []});
    var kilo = new kendo.data.DataSource({data: []});
    var remark = new kendo.data.DataSource({data: []});
    var print_count = new kendo.data.DataSource({data: []});
    var color_code = new kendo.data.DataSource({data: []});
    var bill_code = new kendo.data.DataSource({data: []});
    var finished = new kendo.data.DataSource({data: []});
    var price = new kendo.data.DataSource({data: []});
    var money = new kendo.data.DataSource({data: []});

    var dataSource = new kendo.data.DataSource({
        data: data,
        schema: {
            model: {
                fields: {
                    amount: {type: "number"},
                    kilo: {type: "number"},
                }
            }
        },
        aggregate: [
            {field: "amount", aggregate: "sum"},
            {field: "kilo", aggregate: "sum"},
        ],

        change: function (e) {
            //获取过滤条件
            let filtrer = this.filter();
            //获取所有数据
            let allData = this.data();
            //进行过滤条件的筛选
            let query = new kendo.data.Query(allData);
            //获取过过滤后的数据
            let data = query.filter(filtrer).data;
            iid.data(unique(data, 'iid'));
            client_sid.data(unique(data, 'client_sid'));
            client_name.data(unique(data, 'client_name'));
            order_code.data(unique(data, 'order_code'));
            product_sid.data(unique(data, 'product_sid'));
            color.data(unique(data, 'color'));
            remark.data(unique(data, 'remark'));
            bill_code.data(unique(data, 'bill_code'));
            // B_ProcessName.data(unique(data, 'B_ProcessName'));
            // B_ProcessDate.data(unique(data, 'B_ProcessDate'));
            // taketime.data(unique(data, 'taketime'));

        },
        pageSize: 200,
        requestStart: function (e) {
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text == '查询' || (btn.linkbutton('options').text == '过滤')) {
                    btn.linkbutton('disable')
                }
            })
        },
        requestEnd: function (e) {
            let response = e.response;
            if (response && response['message']) {
                $.messager.show({title: '提示', msg: response['message']})
            }
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text == '查询' || (btn.linkbutton('options').text == '过滤')) {
                    btn.linkbutton('enable')
                }
            })
        }
    });
    var grid = $("#bill_n").kendoGrid({
        sortable: true,
        resizable: true,
        selectable: "multiple row",
        editable: false,
        filterable: true,
        title: "报表",
        columnMenu: {
            sortable: true
        },
        excel: {
            fileName: "计划单序时表.xlsx",
            proxyURL: "",
            allPages: true,
            filterable: true
        },
        pageable: {
            pageSize: 200
        },
        dataSource: dataSource,
        columns: [
            {
                field: "", title: "序号", width: 30,
                template: "<span class='row_number'></span>", width: 40
            },
            {
                field: 'iid',
                title: '缸号',
                footerTemplate: '总计:',
                width: 70,
                // hidden: true,
                filterable: {multi: true, search: true, dataSource: iid},
                footerAttributes: {style: "text-align:right;"},
                attributes: {style: "text-align:center;white-space:nowrap;text-overflow:ellipsis;"},
                headerAttributes: {style: 'text-align:center;'}
            },

            {
                field: "bill_date",
                title: '开单日期',
                width: 110,
                filterable: {multi: true, search: true, dataSource: bill_date},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "client_sid",
                title: '客户编号',
                hidden: true,
                width: 100,
                filterable: {multi: true, search: true, dataSource: client_sid},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: 'client_name',
                title: '客户名称',
                width: 100,
                filterable: {multi: true, search: true, dataSource: client_name},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: 'order_code',
                title: '订单号',
                width: 100,
                filterable: {multi: true, search: true, dataSource: order_code},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: 'product_sid',
                title: '品名',
                width: 200,
                filterable: {multi: true, search: true, dataSource: product_sid},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },

            {
                field: 'color',
                title: '颜色',
                width: 100,
                filterable: {multi: true, search: true, dataSource: color},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            // {
            //     field: 'color_code',
            //     title: '色号',
            //     width: 100,
            //     filterable: {multi: true, search: true, dataSource: color},
            //     attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
            //     headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            // },
            // {
            //     field: 'amount',
            //     title: '件数',
            //     width: 80,
            //     template: item => item['amount'] ? parseFloat(item['amount']).toFixed(0) : '',
            //     format: "{0:n1}",
            //     footerTemplate: '#=kendo.format("{0:n1}",sum)#',
            //     footerAttributes: {style: "text-align:right;"},
            //     attributes: {style: "text-align:right;",'class': 'kendo-custum-number'},
            //     headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            // },
            {
                field: 'kilo',
                title: '计划重量',
                width: 100,
                template: item => item['kilo'] ? parseFloat(item['kilo']).toFixed(1) : '',
                format: "{0:n1}",
                footerTemplate: '#=kendo.format("{0:n1}",sum)#',
                footerAttributes: {style: "text-align:right;"},
                attributes: {style: "text-align:right;",'class': 'kendo-custum-number'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "price",
                title: '平均单价',
                width: 100,
                template: item => item['price'] ? parseFloat(item['price']).toFixed(2) : '',
                filterable: {multi: true, search: true, dataSource: price},
                attributes: {style: 'text-align:right;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "money",
                title: '单缸金额',
                width: 100,
                template: item => item['money'] ? parseFloat(item['money']).toFixed(2) : '',
                filterable: {multi: true, search: true, dataSource: money},
                attributes: {style: 'text-align:right;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "print_count",
                title: '打印数',
                width: 100,
                filterable: {multi: true, search: true, dataSource: print_count},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "remark",
                title: '开单备注',
                width: 200,
                filterable: {multi: true, search: true, dataSource: remark},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "",
                title: '.',
                width: 30,
                filterable: {multi: true, search: true, dataSource: ''},
                attributes: {style: 'white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
        ],
        reorderable: true,
        height: '99%',
        dataBound: function () {
            var rows = this.items();
            $(rows).each(function () {
                var index = $(this).index() + 1;
                var rowLabel = $(this).find(".row_number");
                $(rowLabel).html(index);
            });
            // 行样式
            let kgrid = $("#bill_n").data("kendoGrid");
            let trs = kgrid.tbody.find("tr");
            trs.each(function (i, row) {
                let data = kgrid.dataItem(row);
                let finished = data.finished;
                if (finished === 1) $(row).css("color", "#0f0cd0");
            });
        },
    });
    var grid01 = $("#bill_n").data("kendoGrid");
    //双击事件
    grid.on('dblclick', 'tr', function () {
        //  代码
        // var kGrid = $("#bill_n").data("kendoGrid");
        // var row = kGrid.select();
        // var dataItem = kGrid.dataItem(row);
        // var id = dataItem['iid'];
        // var parent_id = dataItem['parent_id'];
        // $('#B').window('open');  // open a window
        // openPlanNow(parent_id,id)
    });
    //右击事件
    grid.on('contextmenu', 'tr', function (e) {
        e.preventDefault();
        //  代码

    });
    //  单击事件
    grid.on('click', 'tr', function () {
        //  代码

    });
}

//作废数据
function initKendoUiGrid_b(data) {

    var iid = new kendo.data.DataSource({data: []});
    var bill_date = new kendo.data.DataSource({data: []});
    var client_sid = new kendo.data.DataSource({data: []});
    var client_name = new kendo.data.DataSource({data: []});
    var order_code = new kendo.data.DataSource({data: []});
    var product_sid = new kendo.data.DataSource({data: []});
    var color = new kendo.data.DataSource({data: []});
    var amount = new kendo.data.DataSource({data: []});
    var kilo = new kendo.data.DataSource({data: []});
    var remark = new kendo.data.DataSource({data: []});
    var delivery_date = new kendo.data.DataSource({data: []});
    var color_code = new kendo.data.DataSource({data: []});
    var bill_code = new kendo.data.DataSource({data: []});
    var B_ProcessName = new kendo.data.DataSource({data: []});
    var B_ProcessDate = new kendo.data.DataSource({data: []});
    var taketime = new kendo.data.DataSource({data: []});
    var nullify_remark = new kendo.data.DataSource({data: []});

    var dataSource = new kendo.data.DataSource({
        data: data,
        schema: {
            model: {
                fields: {
                    amount: {type: "number"},
                    kilo: {type: "number"},
                }
            }
        },
        aggregate: [
            {field: "amount", aggregate: "sum"},
            {field: "kilo", aggregate: "sum"},
        ],

        change: function (e) {
            //获取过滤条件
            let filtrer = this.filter();
            //获取所有数据
            let allData = this.data();
            //进行过滤条件的筛选
            let query = new kendo.data.Query(allData);
            //获取过过滤后的数据
            let data = query.filter(filtrer).data;
            iid.data(unique(data, 'iid'));
            client_sid.data(unique(data, 'client_sid'));
            client_name.data(unique(data, 'client_name'));
            order_code.data(unique(data, 'order_code'));
            product_sid.data(unique(data, 'product_sid'));
            color.data(unique(data, 'color'));
            remark.data(unique(data, 'remark'));
            bill_code.data(unique(data, 'bill_code'));
            B_ProcessName.data(unique(data, 'B_ProcessName'));
            B_ProcessDate.data(unique(data, 'B_ProcessDate'));
            taketime.data(unique(data, 'taketime'));

        },
        pageSize: 200,
        requestStart: function (e) {
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text == '查询' || (btn.linkbutton('options').text == '过滤')) {
                    btn.linkbutton('disable')
                }
            })
        },
        requestEnd: function (e) {
            let response = e.response;
            if (response && response['message']) {
                $.messager.show({title: '提示', msg: response['message']})
            }
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text == '查询' || (btn.linkbutton('options').text == '过滤')) {
                    btn.linkbutton('enable')
                }
            })
        }
    });
    var grid = $("#bill_b").kendoGrid({
        sortable: true,
        resizable: true,
        selectable: "multiple row",
        editable: false,
        filterable: true,
        title: "报表",
        columnMenu: {
            sortable: true
        },
        excel: {
            fileName: "计划单序时表.xlsx",
            proxyURL: "",
            allPages: true,
            filterable: true
        },
        pageable: {
            pageSize: 200
        },
        dataSource: dataSource,
        columns: [
            {
                field: "", title: "序号", width: 30,
                template: "<span class='row_number'></span>", width: 40
            },
            {
                field: "parent_id",
                hidden: true,
                title: '订单明细主键',
                width: 30,
                attributes: {style: 'white-space:nowrap;'},
                headerAttributes: {style: 'text-align:left;margin-right: 2px;'},
                footerAttributes: {style: 'color: red;text-align:center;'}
            },
            {
                field: 'iid',
                title: '计划单主键',
                footerTemplate: '总计:',
                width: 80,
                hidden: true,
                filterable: {multi: true, search: true, dataSource: iid},
                footerAttributes: {style: "text-align:right;"},
                attributes: {style: "text-align:center;white-space:nowrap;text-overflow:ellipsis;"},
                headerAttributes: {style: 'text-align:center;'}
            },
            {
                field: 'bill_code',
                title: '计划单号',
                footerTemplate: '总计:',
                width: 120,
                filterable: {multi: true, search: true, dataSource: bill_code},
                footerAttributes: {style: "text-align:right;"},
                attributes: {style: "text-align:center;white-space:nowrap;text-overflow:ellipsis;"},
                headerAttributes: {style: 'text-align:center;'}
            },
            {
                field: "bill_date",
                title: '开单日期',
                width: 120,
                filterable: {multi: true, search: true, dataSource: bill_date},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            // {
            //     field: "delivery_date",
            //     title: '交期',
            //     width: 80  ,
            //     filterable: {multi: true, search: true, dataSource: delivery_date},
            //     attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
            //     headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            // },
            {
                field: "client_sid",
                title: '客户编号',
                hidden: true,
                width: 100,
                filterable: {multi: true, search: true, dataSource: client_sid},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: 'client_name',
                title: '客户名称',
                width: 120,
                filterable: {multi: true, search: true, dataSource: client_name},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: 'order_code',
                title: '订单号',
                width: 120,
                filterable: {multi: true, search: true, dataSource: order_code},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: 'product_sid',
                title: '品名',
                width: 180,
                filterable: {multi: true, search: true, dataSource: product_sid},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },

            {
                field: 'color',
                title: '颜色',
                width: 100,
                filterable: {multi: true, search: true, dataSource: color},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            // {
            //     field: 'color_code',
            //     title: '色号',
            //     width: 100,
            //     filterable: {multi: true, search: true, dataSource: color},
            //     attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
            //     headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            // },
            // {
            //     // patternId
            //     field: "color_code",
            //     title: "色块",
            //     width: 50,
            //     attributes: {style: "white-space:nowrap;text-overflow:ellipsis;background-color:#= color_code?color_code:'white'# ;color:#= color_code?color_code:'white'# ;"}
            // },
            // {
            //     field: 'amount',
            //     title: '件数',
            //     width: 80,
            //     template: item => item['amount'] ? parseFloat(item['amount']).toFixed(0) : '',
            //     format: "{0:n1}",
            //     footerTemplate: '#=kendo.format("{0:n1}",sum)#',
            //     footerAttributes: {style: "text-align:right;"},
            //     attributes: {style: "text-align:right;",'class': 'kendo-custum-number'},
            //     headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            // },
            {
                field: 'kilo',
                title: '计划重量',
                width: 120,
                template: item => item['kilo'] ? parseFloat(item['kilo']).toFixed(1) : '',
                format: "{0:n1}",
                footerTemplate: '#=kendo.format("{0:n1}",sum)#',
                footerAttributes: {style: "text-align:right;"},
                attributes: {style: "text-align:right;",'class': 'kendo-custum-number'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "remark",
                title: '开单备注',
                width: 180,
                filterable: {multi: true, search: true, dataSource: remark},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "nullify_remark",
                title: '作废原因',
                width: 180,
                filterable: {multi: true, search: true, dataSource: nullify_remark},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "B_ProcessName",
                title: '当前工序',
                width: 100,
                filterable: {multi: true, search: true, dataSource: B_ProcessName},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "B_ProcessDate",
                title: '刷卡时间',
                width: 120,
                filterable: {multi: true, search: true, dataSource: B_ProcessDate},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "taketime",
                title: '耗时',
                width: 80,
                filterable: {multi: true, search: true, dataSource: taketime},
                attributes: {style: 'text-align:center;white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "finished",
                title: '完工',
                width: 50,
                hidden: true,
                filterable: {multi: true, search: true, dataSource: 'finished'},
                attributes: {style: 'white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
            {
                field: "",
                title: '.',
                width: 100,
                filterable: {multi: true, search: true, dataSource: ''},
                attributes: {style: 'white-space:nowrap;text-overflow:ellipsis;'},
                headerAttributes: {style: 'text-align:center;margin-right: 2px;'}
            },
        ],
        reorderable: true,
        height: '99%',
        dataBound: function () {
            var rows = this.items();
            $(rows).each(function () {
                var index = $(this).index() + 1;
                var rowLabel = $(this).find(".row_number");
                $(rowLabel).html(index);
            });
            // 行样式
            // 行样式
            let kgrid = $("#bill_b").data("kendoGrid");
            let trs = kgrid.tbody.find("tr");
            trs.each(function (i, row) {
                let data = kgrid.dataItem(row);
                $(row).css("color", "red").css("text-decoration", "line-through");
            });
        },
    });
    var grid01 = $("#bill_b").data("kendoGrid");
    //双击事件
    grid.on('dblclick', 'tr', function () {

    });
    //右击事件
    grid.on('contextmenu', 'tr', function (e) {
        e.preventDefault();
        //  代码

    });
    //  单击事件
    grid.on('click', 'tr', function () {
        //  代码

    });
}

//新增
function addplan() {
    $('#A').window('open');  // open a window
    m_PlanID =null;
    addplanA(m_PlanID);
    // window.parent.addTab('白坯计划', 'whiteplan.html?id=0');
}

//刷新
//刷新
function refresh() {
    switch (tabNumber) {
        case 0:
            btn_refresh();
            break;
        case 1:
            btn_refresh_m();
            break;
        case 2:
            btn_refresh_n();
            break;
        case 3:
            btn_refresh_b();
            break;
    }
}

function btn_refresh() {
    var kGrid = $("#bill").data("kendoGrid");
    // var row = kGrid.select();
    // var dataItem = kGrid.dataItem(row);

    var data1 = $('#start').datebox('getValue');
    var data2 = $('#end').datebox('getValue');
    $.ajax({
        url: strLin + 'dynamicQuery/callProc',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(['usp_getPlandetail',data1,data2]),
        contentType: "application/json;charset=UTF-8",
        success: function (cdata) {
            let data=cdata.data;
            console.log(data);
            kGrid.dataSource.data(data);
        }

    });
}
function btn_refresh_m() {
    var kGrid = $("#bill_m").data("kendoGrid");
    // var row = kGrid.select();
    // var dataItem = kGrid.dataItem(row);

    var data1 = $('#start').datebox('getValue');
    var data2 = $('#end').datebox('getValue');
    $.ajax({
        url: strLin + 'dynamicQuery/callProc',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(['usp_getPlandetail_WP']),
        contentType: "application/json;charset=UTF-8",
        success: function (cdata) {
            let data=cdata.data;
            console.log(data);
            kGrid.dataSource.data(data);
        }

    });
}
function btn_refresh_n() {
    var kGrid = $("#bill_n").data("kendoGrid");

    var data1 = $('#start').datebox('getValue');
    var data2 = $('#end').datebox('getValue');
    $.ajax({
        url: strLin + 'dynamicQuery/callProc',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(['usp_getPlandetail_GD',data1,data2]),
        contentType: "application/json;charset=UTF-8",
        success: function (cdata) {
            let data=cdata.data;
            console.log(data);
            kGrid.dataSource.data(data);
        }

    });
}
function btn_refresh_b() {
    var kGrid = $("#bill_b").data("kendoGrid");
    var data1 = $('#start').datebox('getValue');
    var data2 = $('#end').datebox('getValue');

    $.ajax({
        url: strLin + 'dynamicQuery/callProc',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(['usp_getPlandetail_ZF',data1,data2]),
        contentType: "application/json;charset=UTF-8",
        success: function (cdata) {
            let data=cdata.data;
            console.log(data);
            kGrid.dataSource.data(data);
        }

    });
}

function onSelect(title, index) {
    tabNumber=index;
    setStatus(index);
    switch (index) {
        case 0:
            btn_refresh();
            break;
        case 1:
            btn_refresh_m();
            break;
        case 2:
            btn_refresh_n();
            break;
        case 3:
            btn_refresh_b();
            break;
    }
}

//打印
function btn_print() {
    let kGrid = $("#bill").data("kendoGrid");
    if (kGrid == undefined) {
        $.messager.alert('提示', '网格组件异常！请刷新重试！');
        return;
    }
    let filtrer = kGrid.dataSource.filter();
    let allData = kGrid.dataSource.data();
    let query = new kendo.data.Query(allData);
    let data = query.filter(filtrer).data;
    if (data != undefined && data.length > 0) {
        let arr = [];
        for (let i = 0; i < data.length; i++) {
            let item = data[i]['planId'];
            if (item) arr.push(item);
        }
        if (arr.length <= 0) return;
        let reportlets = [];
        let printTemplate = {};
        printTemplate.ids = arr.join(',');
        printTemplate.reportlet = PRINT_TEMPLATE_NAME;
        reportlets.push(printTemplate);
        if (reportlets.length <= 0) {
            $.messager.show({title: '提示', msg: '没有明细！无法打印！'});
            return ;
        }
        let config = {
            url: "ReportServer",
            isPopUp: false,
            data: {
                reportlets: reportlets
            }
        };
        FR.doURLPDFPrint(config);
    }
}

//导出
function btn_export() {
    let kGrid = $("#bill").data("kendoGrid");
    if (kGrid == undefined) {
        $.messager.alert('提示', '网格组件异常！请刷新重试！');
        return;
    }
    kGrid.saveAsExcel();
}

//重置
function btn_reset() {
    $.messager.show('提示', '该页面暂无chongzh业务！');
}

//  清除
function btn_cancel_filter() {
    switch (tabNumber) {
        case 0:
            let kGrid = $("#bill").data("kendoGrid");
            if (kGrid != undefined) {
                kGrid.dataSource.filter({});
            }
            break;
        case 1:
            let kGrid_m = $("#bill_m").data("kendoGrid");
            if (kGrid_m != undefined) {
                kGrid_m.dataSource.filter({});
            }
            break;
        case 2:
            let kGrid_n = $("#bill_n").data("kendoGrid");
            if (kGrid_n != undefined) {
                kGrid_n.dataSource.filter({});
            }
            break;
        case 3:
            let kGrid_b = $("#bill_b").data("kendoGrid");
            if (kGrid_b != undefined) {
                kGrid_b.dataSource.filter({});
            }
            break;
    }
}

//右键菜单弹出框
function menuHandler() {

}


function getDate(param) {
    if (isNaN(param)) {
        param = 0;
    }
    var curDate = new Date();
    var newDate = new Date(curDate);
    newDate.setDate(curDate.getDate() + param);
    var year = newDate.getFullYear();
    var month = newDate.getMonth() + 1 < 10 ? '0' + (newDate.getMonth() + 1) : newDate.getMonth() + 1;
    var date = newDate.getDate() < 10 ? '0' + newDate.getDate() : newDate.getDate();
    var separator = '-';
    return year + separator + month + separator + date;
}

//关闭当前标签页事件
function closeTab() {
    window.parent.closeTab();
}

//获取报表对应的起始日期
function getreportStartDate() {
    $.ajax({
        url: strLin + '/configAll/all',
        type: 'get',
        dataType: 'json',
        success: function (cdata) {
            let  data=cdata.data;
            console.log(data);
            data1=data.filter(item=>item.config_name==="计划序时表_pc");
            var starDate=getDay(-data1[0].value_num);
            console.log(starDate);
            $("#start").datebox("setValue", starDate);

            // initKendoUiGrid();
            btn_refresh();

        }
    })
}

//颜色格式化
function styler(v, r, i) {
    return v ? "background-color:" + v + ";color:" + v + ";" : "";
}

//报表中打印计划单
function printplanbill2() {
    var kGrid = $("#bill_n").data("kendoGrid");
    var row = kGrid.select();
    var dataItem = kGrid.dataItem(row);
    var id = dataItem['iid'];
    if (id>0) {
        uopdateprintnumber(id);
        console.log('打印缸单');
        var strurl = strLinprint + "ReportServer?reportlet=report_plan.cpt&billID=" + id;
        window.open(strurl);
    }
}


//修改打印次数
function uopdateprintnumber(planid) {
    $.ajax({
        url: strLin + 'dynamicQuery/callProc',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(['usp_updateplanprintcount',planid]),
        contentType: "application/json;charset=UTF-8",
        success: function (cdata) {
          //修改打印次数
            refresh();
        }

    });
}

//生成新的缸单
function createnewGD() {
    let m_color;
    let color_judge=true;
    var selectedItem=[];
    var kGrid = $("#bill_m").data("kendoGrid");
    var row = kGrid.select();
    // var dataItem = kGrid.dataItem(row);
    // var id = dataItem['iid'];
    row.each(function(index, row) {
        selectedItem[index] = {"iid":kGrid.dataItem(row)['iid']};
        if (index>0){
            if (m_color!=kGrid.dataItem(row)['color']){
                $.messager.show({title: '提示', msg: '颜色不同，无法生产缸单！'});
                color_judge=false;
                return false;
            }
        }else{
            m_color=kGrid.dataItem(row)['color']
        }
    });

    //判断所选计划是否是相同颜色
    if (color_judge==true){
        $.messager.defaults = {ok: "确定", cancel: "取消", width: 300, closable: false};
        $.messager.confirm('确认', '您确定当前选中的 '+ selectedItem.length + " 个规格的计划单生成缸单吗？", function (r) {
            if (r) {

                $('#A').window('open')

            }
        });
    }
}

//缸单作废
function gdzf() {
    var kGrid = $("#bill_n").data("kendoGrid");
    var row = kGrid.select();
    var dataItem = kGrid.dataItem(row);
    var id = dataItem['iid'];
    var finished = dataItem['finished'];

    if (finished==1){
        $.messager.alert('提示','缸单已完工，不允许作废！')
        return;
    }

    $.messager.defaults = {ok: "确定", cancel: "取消", width: 300, closable: false};
    $.messager.confirm('确认', '您确认要作废当前选中缸单吗？', function (r) {
        if (r) {

            let SQLstr={"iid": id,"state": 0};
            $.ajax({
                url: strLin + 'planAssemble/updateByPrimaryKeySelective',
                type: 'POST',
                dataType: 'json',
                data: JSON.stringify(SQLstr),
                contentType: "application/json;charset=UTF-8",
                success: function (data) {
                    console.log(data);
                    $.messager.alert('提示','缸单作废完成！')
                    refresh();
                }
            });
        }
    });

}

function SaveA() {
    var selectedItem=[];
    var kGrid = $("#bill_m").data("kendoGrid");
    var row = kGrid.select();
    var money=$("#accountqtyA").textbox('getValue');
    row.each(function(index, row) {
        selectedItem[index] = {"iid":kGrid.dataItem(row)['iid']};
    });

    let strSQL={"main":{"money":isNull(money,0)},"details":selectedItem};
    console.log(strSQL);
    $.ajax({
        url: strLin + 'planAssemble/save',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(strSQL),
        contentType: "application/json;charset=UTF-8",
        success: function (cdata) {
            let data=cdata.data;
            console.log(data);
            console.log('缸单生成完成！');
            $.messager.show({title: '提示', msg: '生成缸单成功！！'});
            // kGrid.dataSource.data(data);
            refresh();
            cancelA();
        }
    });
}

//调整取消保存
function cancelA() {
    $('#A').window('close');
    $("#accountqtyA").textbox("setValue", '');
}

function setStatus(index) {
    let msg = {
        'tab0': ['生成缸单','打印缸单','作废缸单'],
        'tab1': ['打印缸单','作废缸单'],
        'tab2': ['生成缸单'],

        'tab0-1': ['取消完工'],
        'tab1-1': [''],
        'tab1-1.1': ['取消完工'],
        'tab1-1.2': ['设置完工'],
        'tab2-1': ['生成计划单'],
    };
    setTimeout(function () {
        $('#north').find('a[class*="easyui-linkbutton"]').each(function (item, i) {
            let text = $(i).linkbutton('options').text;
            if (msg[`tab${index}`] == undefined) return false;
            if (msg[`tab${index}`].indexOf(text) > -1) {
                $(i).linkbutton('disable');
            } else {
                $(i).linkbutton('enable');
            }
        })
    }, 0);
}
